// Copyright 2018 The QOS Authors

package handler

import (
	"log"
	"net/http"
	"os"

	"gitee.com/jsonxt/rest_mail/utils"
	"github.com/gin-gonic/gin"
)

var wechatAgentID string
var wechatCorID string
var wechatSecret string

func init() {
	wechatAgentID = os.Getenv("WechatAgentID")
	wechatCorID = os.Getenv("WechatCorID")
	wechatSecret = os.Getenv("WechatSecret")
}

type WechatReq struct {
	App     string `form:"app" json:"app"`
	Content string `form:"content" json:"content"`
}

func WechatHander(c *gin.Context) {
	var body WechatReq
	if err := c.Bind(&body); err != nil {
		c.JSON(http.StatusBadRequest, map[string]string{"message": "请求参数错误" + err.Error()})
		return
	}

	if err := SendWechat(body.App, body.Content); err != nil {
		log.Printf("send mail err:%+v", err)
		c.JSON(http.StatusInternalServerError, map[string]string{"message": "发送失败" + err.Error()})
		return
	}

	c.JSON(http.StatusOK, nil)

}

// SendWechat 发送wechat消息
func SendWechat(app, content string) error {
	return utils.WechatMessage(wechatAgentID, wechatCorID, wechatSecret, "", content)
}
